<?php

Route::get("demo",function($haha=""){
	// dd(Archive::find(1)->tag);
	// dd(Auth::user()->archive()->find(10)->tag);
	// echo  __("validation.accepted");
	echo Theme;
});



Route::get("[/]?(:num)?","home@index");
Route::get("archive/(:num)",array("before"=>"view","uses"=>"archive@index"));
Route::post("comment",array("before"=>"auth","uses"=>"comment@index"));
Route::get("notification",array("before"=>"auth","uses"=>"notification@index"));
Route::get("favorite",array("before"=>"auth","uses"=>"favorite@index"));


Route::get("setting",array("before"=>"auth",function(){
	return View::make("home.setting");
}));

Route::post("setting",array("before"=>"auth",function(){
		Auth::user()->email           = Input::get("email");
		Auth::user()->userinfo->url   = Input::get("url");
		Auth::user()->userinfo->about = Input::get("about");
		Auth::user()->userinfo->save();
		Auth::user()->save();
		return Redirect::to('setting');
}));

Route::post("upload[/]?(post)?",array("before"=>"auth",function($post=null){
	$rules = array(
		'imgfile' => 'image|max:300'
	);

	$filename=Input::file("imgfile.name");
	if (!is_null($filename)){
		$validator = Validator::make(Input::file(),$rules);
		if ($validator->fails()){
			return json_encode(array("error"=>"fails","msg"=>"请上传图片,且最大300k！"));
		}else{
			$savename = uniqid() . '.' . File::extension($filename);
			if ( is_null($post) ){
				$success = Resizer::open(Input::file('imgfile'))
					        ->resize( 48 , 48 , 'crop' )
					        ->save(path("public") . 'img/' . $savename, 100);
				Auth::user()->userinfo->avatar = URL::base() . '/img/' . $savename;
				Auth::user()->userinfo->save();
			}else{
				Input::upload('imgfile',path("public") . 'img' , $savename);
			}
			return json_encode(array("error"=>"succ","msg"=>URL::base() . '/img/' . $savename));
		}
	}
}));


Route::get("login",function(){
	return View::make("home.login");
});

Route::get("logout",function(){
	if ( !Auth::guest() )
		Auth::logout();
	return Redirect::to("/");
});

Route::post("login",function(){
	if ( Session::token() == Input::get("csrf_token") ){
		if ( Input::get("code") == Session::get("code")  ){
			$user=array(
									"username" => Input::get("uname"),
									"password" => Input::get("upassword")
								);
			if (Auth::attempt($user)){
				return Redirect::to('/');
			}
		}
	}
	return Redirect::to('login');
});

Route::get("code.png",function(){
	$code=null;
	for ($i=0;$i<4;$i++){
			$code.=rand(0,9);
		}
	Session::put("code",$code);
	$new_image = imagecreatetruecolor(75, 27); //(Panjang, Tinggi)
	$color = imagecolorAllocate($new_image,255,255,255);
	imagefill($new_image,0,0,$color);
	$line_color = imagecolorallocate($new_image,233,239,239); //line color
	$text_color = imagecolorallocate($new_image, 0, 0, 0); //text color-white
	imagestring($new_image, 6, 11, 6, $code, $text_color);// Draw a random string horizontally
	imagepng($new_image);
	imagedestroy($new_image);
	return Response::make(null, 200, array('Content-Type' => "image/png"));
});

Route::get("post/(:num?)",array('before' => 'auth',function($select=1){
	foreach (Categorie::all() as $cate) {
		$categorie[$cate->id]=$cate->title;
	}
	return View::make("home.post")->with(array("categorie"=>$categorie,"select"=>$select));
}));

Route::post("post/(:num?)",array('before' => 'auth',"uses"=>"archive@index"));







Route::get("register",function(){
	return View::make("home.register");
});

Route::post("register",function(){
	if ( Session::token() == Input::get("csrf_token") ){
		$rules = array(
								"upass" => "required|min:6",
								"uname" => "required|min:6"
							);
		$validator = Validator::make(Input::all(),$rules);
		if ( !$validator->fails() ){
			$ret=User::insert(array(
										"upass" => Hash::make(Input::get("upassword")),
										"uname" => Input::get("uname")
									));
			if ($ret)
				echo "注册成功！";
		}else{
			echo "注册信息不符合规则！";
		}
	}else{
		echo "系统禁止跨站提交！";
	}
});




/*测试用*/
/*Route::get("addcate",function(){
	categorie::insert(array("title"=>"J++","about"=>"J shap lang","top"=>"0","lock"=>"0","archives"=>"0"));
});*/



Event::listen('404', function()
{
	return Response::error('404');
});

Event::listen('500', function($exception)
{
	return Response::error('500');
});


Route::filter('before', function()
{
	// Do stuff before every request to your application...
});

Route::filter('after', function($response)
{
	// Do stuff after every request to your application...
});

Route::filter('csrf', function()
{
	if (Request::forged()) return Response::error('500');
});

Route::filter('auth', function()
{
	if (Auth::guest()) return Redirect::to('login');
});

Route::filter('image', function($response)
{
    $response->header('Content-Type', 'image/jpeg');
});

Route::filter('view', function()
{
	$archive = Archive::find( URI::segment(2) );
	$archive->view = (int)$archive->view+1;
	$archive->save();
});

Event::listen('laravel.query', function($sql) {
	// echo "<pre>" . var_dump($sql) . "</pre>";
});